package com.oss.robot.project;

import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.HasSiblingFilter;
import org.htmlparser.filters.LinkStringFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;

import com.oss.model.Project;
import com.oss.robot.BaseRobot;

/**
 * @author xueqiang.mi
 * @since 2010-7-25
 */
@Deprecated
public class _ApacheRobot extends BaseRobot {

	static public final String URL = "http://apache.org/";
	static public final String SOURCE = "Apache.org";
	static public final String LICENSE = "Apache License 2.0";
	static public final Integer TASK_NUMBER_PER_TIME = 1;

	public void retrieveDescriptions() throws Exception {
		final String url = "http://projects.apache.org/indexes/alpha.html";
		Parser parser = new Parser(url);
		NodeList nodeList = parser.extractAllNodesThatMatch(new AndFilter(
				new LinkStringFilter("/"), new AndFilter(new HasParentFilter(
						new TagNameFilter("li")), new HasSiblingFilter(
						new TagNameFilter("div")))));

		List<Project> prjs = prjService.getPrjsBySource(SOURCE, 1, 200);
		for (Node node : nodeList.toNodeArray()) {
			LinkTag linkTag = (LinkTag) node;
			String prjName = linkTag.getFirstChild().getText();
			for (Project prj : prjs) {
				if (prjName.equals("Apache " + prj.getName())
						|| prjName.equals(prj.getName())) {
					// 更新项目描述信息
					Node n = node.getNextSibling();
					while (!(n instanceof Div)) {
						n = n.getNextSibling();
					}

					if (n.getFirstChild() != null) {
						String desc = n.getFirstChild().getText().trim();
						prj.setDescription(desc);
						prjService.save(prj);
					} else {
						// 该页面有存在描述为空的项目，这里忽略掉，比如Apache Directory
					}
					// 从列表中删除该项目
					prjs.remove(prj);
					break;
				}
			}
		}
	}

	public void retrieveProjects() throws Exception {
		Parser parser = new Parser(URL);
		NodeList nodeList = parser.extractAllNodesThatMatch(new AndFilter(
				new TagNameFilter("ul"), new HasSiblingFilter(
						new TagNameFilter("div"))));
		nodeList = nodeList.extractAllNodesThatMatch(new LinkStringFilter("/"),
				true);
		for (Node node : nodeList.toNodeArray()) {
			LinkTag linkTag = (LinkTag) node;
			String name = linkTag.getFirstChild().getText();
			String url = linkTag.getLink();
			Project prj = new Project(name, LICENSE, SOURCE, url);
			prj.setTag("others");
			prjService.add(prj);
		}
	}
}
